<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="0 引言好记性不如烂笔头，把常见的一些 MySQL 索引失效的问题记录下来，在工作中可以时时检查对比。 主要分为两个部分，explain 介绍和各种索引失效场景的模拟。 建表语句123456789101112CREATE TABLE &#96;people&#96; (  &#96;id&#96; int unsigned NOT NULL AUTO_INCREMENT,  &#96;name&#96; varchar(30) CHARACTE">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL索引失效的原因记录总结">
<meta property="og:url" content="http://littlefxc.github.io/2022/03/22/docs/MySQL/MySQL%E7%B4%A2%E5%BC%95%E5%A4%B1%E6%95%88%E7%9A%84%E5%8E%9F%E5%9B%A0%E8%AE%B0%E5%BD%95%E6%80%BB%E7%BB%93/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="0 引言好记性不如烂笔头，把常见的一些 MySQL 索引失效的问题记录下来，在工作中可以时时检查对比。 主要分为两个部分，explain 介绍和各种索引失效场景的模拟。 建表语句123456789101112CREATE TABLE &#96;people&#96; (  &#96;id&#96; int unsigned NOT NULL AUTO_INCREMENT,  &#96;name&#96; varchar(30) CHARACTE">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-21T22:55:56.000Z">
<meta property="article:modified_time" content="2022-05-10T01:22:58.400Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="mysql">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/2022/03/22/docs/MySQL/MySQL%E7%B4%A2%E5%BC%95%E5%A4%B1%E6%95%88%E7%9A%84%E5%8E%9F%E5%9B%A0%E8%AE%B0%E5%BD%95%E6%80%BB%E7%BB%93/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>MySQL索引失效的原因记录总结 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#0-%E5%BC%95%E8%A8%80"><span class="nav-text">0 引言</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BB%BA%E8%A1%A8%E8%AF%AD%E5%8F%A5"><span class="nav-text">建表语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#explain-%E6%89%A7%E8%A1%8C%E8%AE%A1%E5%88%92%E5%90%84%E5%B1%9E%E6%80%A7%E8%A7%A3%E9%87%8A%E8%AF%B4%E6%98%8E"><span class="nav-text">explain 执行计划各属性解释说明</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#id"><span class="nav-text">id</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#select-type"><span class="nav-text">select_type</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#type"><span class="nav-text">type</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#possible-keys"><span class="nav-text">possible_keys</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#key"><span class="nav-text">key</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#key-len"><span class="nav-text">key_len</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rows"><span class="nav-text">rows</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#filtered"><span class="nav-text">filtered</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#extra"><span class="nav-text">extra</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%B8%8D%E7%AC%A6%E5%90%88%E6%9C%80%E5%B7%A6%E5%89%8D%E7%BC%80%E5%8E%9F%E5%88%99"><span class="nav-text">1 不符合最左前缀原则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%9C%A8%E7%B4%A2%E5%BC%95%E5%88%97%E4%B8%8A%E6%9C%89%E5%A4%9A%E4%BD%99%E6%93%8D%E4%BD%9C%EF%BC%8C%E5%A6%82%EF%BC%9A%E5%87%BD%E6%95%B0%E3%80%81%E8%AE%A1%E7%AE%97%E3%80%81%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-text">2 在索引列上有多余操作，如：函数、计算、类型转换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6%E6%9C%89-%E3%80%81-gt-%E3%80%81-lt-%E7%AD%89"><span class="nav-text">3 查询条件有!&#x3D;、&gt;    、&lt;等</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-like%E4%BB%A5%E9%80%9A%E9%85%8D%E7%AC%A6-%E4%B8%BA%E5%BC%80%E5%A4%B4"><span class="nav-text">4 like以通配符%为开头</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E4%BD%BF%E7%94%A8%E6%9D%A5%E4%BA%86or"><span class="nav-text">5 使用来了or</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E4%BD%BF%E7%94%A8%E4%BA%86is-null%E6%88%96%E8%80%85is-not-null"><span class="nav-text">6 使用了is null或者is not null</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2022/03/22/docs/MySQL/MySQL%E7%B4%A2%E5%BC%95%E5%A4%B1%E6%95%88%E7%9A%84%E5%8E%9F%E5%9B%A0%E8%AE%B0%E5%BD%95%E6%80%BB%E7%BB%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MySQL索引失效的原因记录总结
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-22 06:55:56" itemprop="dateCreated datePublished" datetime="2022-03-22T06:55:56+08:00">2022-03-22</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:22:58" itemprop="dateModified" datetime="2022-05-10T09:22:58+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="0-引言"><a href="#0-引言" class="headerlink" title="0 引言"></a>0 引言</h2><p>好记性不如烂笔头，把常见的一些 MySQL 索引失效的问题记录下来，在工作中可以时时检查对比。</p>
<p>主要分为两个部分，explain 介绍和各种索引失效场景的模拟。</p>
<h3 id="建表语句"><a href="#建表语句" class="headerlink" title="建表语句"></a>建表语句</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `people` (</span><br><span class="line">  `id` <span class="type">int</span> unsigned <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  `name` <span class="type">varchar</span>(<span class="number">30</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_0900_ai_ci <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">  `gender` tinyint unsigned <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;性别，0男1女&#x27;</span>,</span><br><span class="line">  `career` <span class="type">varchar</span>(<span class="number">30</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_0900_ai_ci <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;技能&#x27;</span>,</span><br><span class="line">  `skills` <span class="type">varchar</span>(<span class="number">30</span>) <span class="keyword">COLLATE</span> utf8mb4_general_ci <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;技能&#x27;</span>,</span><br><span class="line">  `birthday` <span class="type">date</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;出生日期&#x27;</span>,</span><br><span class="line">  `lifetime` <span class="type">int</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;寿命&#x27;</span>,</span><br><span class="line">  `gmt_create` <span class="type">timestamp</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;入库时间&#x27;</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (`id`),</span><br><span class="line">  KEY `idx_career_skills_lifetime` (`career`,`skills`,`lifetime`) <span class="keyword">USING</span> BTREE</span><br><span class="line">) ENGINE<span class="operator">=</span>InnoDB AUTO_INCREMENT<span class="operator">=</span><span class="number">26</span> <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8mb4 <span class="keyword">COLLATE</span><span class="operator">=</span>utf8mb4_general_ci;</span><br></pre></td></tr></table></figure>

<h3 id="explain-执行计划各属性解释说明"><a href="#explain-执行计划各属性解释说明" class="headerlink" title="explain 执行计划各属性解释说明"></a><code>explain</code> 执行计划各属性解释说明</h3><h4 id="id"><a href="#id" class="headerlink" title="id"></a>id</h4><p>SELECT 识别符。这是 SELECT 的查询序列号，SQL 的执行顺序：</p>
<ol>
<li>id 相同时，执行顺序自上而下。</li>
<li>如果是子查询，id 会递增，id 值越大优先级越高，越先被执行。</li>
<li>id 如果相同，可以认为是一组，自上而下顺序执行；再所有组中，优先级越高，越先被执行。</li>
</ol>
<h4 id="select-type"><a href="#select-type" class="headerlink" title="select_type"></a>select_type</h4><table>
<thead>
<tr>
<th>select_type</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>SIMPLE</td>
<td>简单查询，不使用UNION或子查询</td>
</tr>
<tr>
<td>PRIMARY</td>
<td>最外层查询，查询中若包含任何复杂的子部分，最外层的 select 被标记为 PRIMARY</td>
</tr>
<tr>
<td>SUBQUERY</td>
<td>子查询中的第一个select，结果不依赖于外部查询</td>
</tr>
<tr>
<td>DEPENDENT SUBQUERY</td>
<td>子查询中的第一个select，结果依赖于外部查询</td>
</tr>
<tr>
<td>UNCACHEABLE SUBQUERY</td>
<td>一个子查询的结果不能被缓存，必需重新评估外链表的第一行</td>
</tr>
<tr>
<td>DERIVED</td>
<td>子查询，派生表的 select，from 子句的的子查询</td>
</tr>
<tr>
<td>UNION</td>
<td>联合，UNION 中的第二个或后面的SELECT语句</td>
</tr>
<tr>
<td>UNION RESULT</td>
<td>使用联合的结果</td>
</tr>
<tr>
<td>DEPENDENT UNION</td>
<td>UNION中的第二个或后面的SELECT语句，取决于外面的查询</td>
</tr>
</tbody></table>
<h4 id="type"><a href="#type" class="headerlink" title="type"></a>type</h4><table>
<thead>
<tr>
<th>type</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>ALL</td>
<td>全数据表扫描</td>
</tr>
<tr>
<td>index</td>
<td>全索引表扫描</td>
</tr>
<tr>
<td>RANGE</td>
<td>对索引列进行范围查询</td>
</tr>
<tr>
<td>INDEX_MERGE</td>
<td>合并索引，使用多个单列索引查询</td>
</tr>
<tr>
<td>REF</td>
<td>根据索引查找一个或多个值</td>
</tr>
<tr>
<td>EQ_REF</td>
<td>搜索时使用 primary key 或 unique 类型</td>
</tr>
<tr>
<td>CONST</td>
<td>常量，表最多有一个匹配行,因为仅有一行,在这行的列值可被优化器剩余部分认为是常数,const表很快,因为它们只读取一次。</td>
</tr>
<tr>
<td>SYSTEM</td>
<td>系统，表仅有一行(=系统表)。这是const联接类型的一个特例。</td>
</tr>
</tbody></table>
<ul>
<li><p>性能：all  &lt;  index  &lt;  range  &lt;  index_merge  &lt;  ref_or_null  &lt;  ref  &lt;  eq_ref  &lt;  system/const</p>
</li>
<li><p>性能在 range 之下基本都可以进行调优</p>
</li>
</ul>
<h4 id="possible-keys"><a href="#possible-keys" class="headerlink" title="possible_keys"></a>possible_keys</h4><p>可能使用的索引</p>
<h4 id="key"><a href="#key" class="headerlink" title="key"></a>key</h4><p>真实使用的索引</p>
<h4 id="key-len"><a href="#key-len" class="headerlink" title="key_len"></a>key_len</h4><p>MySQL中使用索引字节长度</p>
<h4 id="rows"><a href="#rows" class="headerlink" title="rows"></a>rows</h4><p>mysql 预估为了找到所需的行而要读取的行数</p>
<h4 id="filtered"><a href="#filtered" class="headerlink" title="filtered"></a>filtered</h4><p>按表条件过滤的行百分比</p>
<h4 id="extra"><a href="#extra" class="headerlink" title="extra"></a>extra</h4><table>
<thead>
<tr>
<th align="left">extra</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Using index</td>
<td align="left">此值表示mysql将使用覆盖索引，以避免访问表。</td>
</tr>
<tr>
<td align="left">Using where</td>
<td align="left">mysql 将在存储引擎检索行后再进行过滤，许多where条件里涉及索引中的列，当(并且如果)它读取索引时，就能被存储引擎检验，因此不是所有带where子句的查询都会显示“Using where”。有时“Using where”的出现就是一个暗示：查询可受益于不同的索引。</td>
</tr>
<tr>
<td align="left">Using temporary</td>
<td align="left">mysql 对查询结果排序时会使用临时表。常见于排序和分组查询<code>group by</code>,<code>order by</code></td>
</tr>
<tr>
<td align="left">Using filesort</td>
<td align="left">当Query中包含 order by 操作，而且无法利用索引完成的排序操作称为“文件排序”。</td>
</tr>
<tr>
<td align="left">Range checked for each record(index map: N)</td>
<td align="left">没有好用的索引，新的索引将在联接的每一行上重新估算，N是显示在possible_keys列中索引的位图，并且是冗余的</td>
</tr>
<tr>
<td align="left">…</td>
<td align="left">…</td>
</tr>
</tbody></table>
<h2 id="1-不符合最左前缀原则"><a href="#1-不符合最左前缀原则" class="headerlink" title="1 不符合最左前缀原则"></a>1 不符合最左前缀原则</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mysql<span class="operator">&gt;</span> explain <span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> people <span class="keyword">where</span> `lifetime` <span class="operator">=</span> <span class="number">23</span> <span class="keyword">and</span> `skills` <span class="operator">=</span> <span class="string">&#x27;口才&#x27;</span>;</span><br><span class="line"><span class="operator">+</span><span class="comment">----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+</span></span><br><span class="line"><span class="operator">|</span> id <span class="operator">|</span> select_type <span class="operator">|</span> <span class="keyword">table</span>  <span class="operator">|</span> partitions <span class="operator">|</span> type <span class="operator">|</span> possible_keys <span class="operator">|</span> key  <span class="operator">|</span> key_len <span class="operator">|</span> <span class="keyword">ref</span>  <span class="operator">|</span> <span class="keyword">rows</span> <span class="operator">|</span> filtered <span class="operator">|</span> Extra       <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+</span></span><br><span class="line"><span class="operator">|</span>  <span class="number">1</span> <span class="operator">|</span> SIMPLE      <span class="operator">|</span> people <span class="operator">|</span> <span class="keyword">NULL</span>       <span class="operator">|</span> <span class="keyword">ALL</span>  <span class="operator">|</span> <span class="keyword">NULL</span>          <span class="operator">|</span> <span class="keyword">NULL</span> <span class="operator">|</span> <span class="keyword">NULL</span>    <span class="operator">|</span> <span class="keyword">NULL</span> <span class="operator">|</span>   <span class="number">14</span> <span class="operator">|</span>     <span class="number">7.14</span> <span class="operator">|</span> <span class="keyword">Using</span> <span class="keyword">where</span> <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+</span></span><br><span class="line"><span class="number">1</span> <span class="type">row</span> <span class="keyword">in</span> <span class="keyword">set</span>, <span class="number">1</span> warning (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>

<p>说明：</p>
<ul>
<li><p>最左前缀原则指的是从索引最左前列开始并且不跳过索引中的列</p>
</li>
<li><p>type 是 ALL, 表示查询语句是全表数据查询，where 的查询条件是 lifetime 和 skills，缺少 career 这个索引条件，无法命中索引 idx_career_skills_lifetime。</p>
</li>
</ul>
<h2 id="2-在索引列上有多余操作，如：函数、计算、类型转换"><a href="#2-在索引列上有多余操作，如：函数、计算、类型转换" class="headerlink" title="2 在索引列上有多余操作，如：函数、计算、类型转换"></a>2 在索引列上有多余操作，如：函数、计算、类型转换</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">mysql<span class="operator">&gt;</span> explain <span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> people <span class="keyword">where</span> <span class="keyword">left</span>(career, <span class="number">2</span>) <span class="operator">=</span> <span class="string">&#x27;群众&#x27;</span>;</span><br><span class="line"><span class="operator">+</span><span class="comment">----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+</span></span><br><span class="line"><span class="operator">|</span> id <span class="operator">|</span> select_type <span class="operator">|</span> <span class="keyword">table</span>  <span class="operator">|</span> partitions <span class="operator">|</span> type <span class="operator">|</span> possible_keys <span class="operator">|</span> key  <span class="operator">|</span> key_len <span class="operator">|</span> <span class="keyword">ref</span>  <span class="operator">|</span> <span class="keyword">rows</span> <span class="operator">|</span> filtered <span class="operator">|</span> Extra       <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+</span></span><br><span class="line"><span class="operator">|</span>  <span class="number">1</span> <span class="operator">|</span> SIMPLE      <span class="operator">|</span> people <span class="operator">|</span> <span class="keyword">NULL</span>       <span class="operator">|</span> <span class="keyword">ALL</span>  <span class="operator">|</span> <span class="keyword">NULL</span>          <span class="operator">|</span> <span class="keyword">NULL</span> <span class="operator">|</span> <span class="keyword">NULL</span>    <span class="operator">|</span> <span class="keyword">NULL</span> <span class="operator">|</span>   <span class="number">14</span> <span class="operator">|</span>   <span class="number">100.00</span> <span class="operator">|</span> <span class="keyword">Using</span> <span class="keyword">where</span> <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+</span></span><br></pre></td></tr></table></figure>

<p>说明：</p>
<ul>
<li><code>LEFT()</code>函数是一个字符串函数，它返回具有指定长度的字符串的左边部分。</li>
<li><code>LEFT()</code>函数的语法：<code>LEFT(str,length);</code><ul>
<li><code>str</code>是要提取子字符串的字符串。</li>
<li><code>length</code>是一个正整数，指定将从左边返回的字符数。</li>
<li><code>LEFT()</code>函数返回<code>str</code>字符串中最左边的长度字符。</li>
<li>如果<code>str</code>或<code>length</code>参数为<code>NULL</code>，则返回<code>NULL</code>值。</li>
<li>如果<code>length</code>为<code>0</code>或为负，则<code>LEFT</code>函数返回一个空字符串。</li>
<li>如果<code>length</code>大于<code>str</code>字符串的长度，则<code>LEFT</code>函数返回整个<code>str</code>字符串。</li>
<li>请注意，<code>SUBSTRING</code> 或 <code>SUBSTR</code> 函数也提供与<code>LEFT</code>函数相同的功能。</li>
</ul>
</li>
<li>career 字段是有索引的，但是执行计划中看到的却没有命中，说明在索引字段上添加多余操作会使其失效</li>
</ul>
<h2 id="3-查询条件有-、-gt-、-lt-等"><a href="#3-查询条件有-、-gt-、-lt-等" class="headerlink" title="3 查询条件有!=、&gt;    、&lt;等"></a>3 查询条件有<code>!=</code>、<code>&gt;</code>    、<code>&lt;</code>等</h2><h2 id="4-like以通配符-为开头"><a href="#4-like以通配符-为开头" class="headerlink" title="4 like以通配符%为开头"></a>4 <code>like</code>以通配符<code>%</code>为开头</h2><h2 id="5-使用来了or"><a href="#5-使用来了or" class="headerlink" title="5 使用来了or"></a>5 使用来了<code>or</code></h2><h2 id="6-使用了is-null或者is-not-null"><a href="#6-使用了is-null或者is-not-null" class="headerlink" title="6 使用了is null或者is not null"></a>6 使用了<code>is null</code>或者<code>is not null</code></h2>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/mysql/" rel="tag"># mysql</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/18/docs/%E5%BE%AE%E6%9C%8D%E5%8A%A1/Redis/" rel="prev" title="Redis">
                  <i class="fa fa-chevron-left"></i> Redis
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/04/01/docs/JVM/JVM-%E6%97%A5%E5%BF%97/" rel="next" title="JVM 日志">
                  JVM 日志 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
